-
Notifications
You must be signed in to change notification settings - Fork 745
/
CollectionExtensions.cs
341 lines (305 loc) · 16.4 KB
/
CollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.Entities.Urls
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using DotNetNuke.Collections.Internal;
public static class CollectionExtensions
{
public static void LoadFromXmlFile(this Dictionary<int, List<ParameterReplaceAction>> actions, string fileName, int portalId, bool portalSpecific, ref List<string> messages)
{
if (messages == null)
{
throw new ArgumentNullException("messages");
}
messages = new List<string>();
if (File.Exists(fileName))
{
var rdr = new XmlTextReader(fileName)
{
XmlResolver = null,
DtdProcessing = DtdProcessing.Prohibit,
};
while (rdr.Read())
{
switch (rdr.NodeType)
{
case XmlNodeType.Element:
if (rdr.Name == "parameterReplace")
{
// now set up the action
string portalIdRaw = rdr.GetAttribute("portalId");
int rulePortalId = -1;
if (portalIdRaw != null)
{
int.TryParse(portalIdRaw, out rulePortalId);
}
// 807 : if portal specific then import all regardless of portal id specified
if (rulePortalId == portalId || rulePortalId == -1 || portalSpecific)
{
int actionCount = 0;
string tabIdRaw = rdr.GetAttribute("tabIds") ?? rdr.GetAttribute("tabId");
string tabNames = rdr.GetAttribute("tabNames");
string name = rdr.GetAttribute("name");
List<int> tabIds = XmlHelpers.TabIdsFromAttributes(tabIdRaw, tabNames, portalId, ref messages);
foreach (int tabId in tabIds)
{
var action = new ParameterReplaceAction
{
LookFor = rdr.GetAttribute("lookFor"),
ReplaceWith = rdr.GetAttribute("replaceWith"),
PortalId = portalId,
Name = name,
TabId = tabId,
};
string changeToSiteRootRaw = rdr.GetAttribute("changeToSiteRoot");
bool changeToSiteRoot;
bool.TryParse(changeToSiteRootRaw, out changeToSiteRoot);
action.ChangeToSiteRoot = changeToSiteRoot;
List<ParameterReplaceAction> tabActionCol;
if (actions.ContainsKey(action.TabId))
{
tabActionCol = actions[action.TabId];
}
else
{
tabActionCol = new List<ParameterReplaceAction>();
actions.Add(action.TabId, tabActionCol);
}
tabActionCol.Add(action);
actionCount++;
messages.Add(name + " replace actions added:" + actionCount.ToString());
}
}
}
break;
case XmlNodeType.EndElement:
break;
}
}
rdr.Close();
}
else
{
messages.Add("File not Found: " + fileName);
}
}
/// <summary>Returns all the redirect rules for the specified portal.</summary>
/// <param name="actions"></param>
/// <param name="fileName"></param>
/// <param name="portalId"></param>
/// <param name="portalSpecific">If true, all rules belong to supplied portalId, even if not specified.</param>
/// <param name="messages"></param>
/// <remarks>807 : change to allow specificatoin of assumption that all rules belong to the supplied portal.</remarks>
public static void LoadFromXmlFile(this Dictionary<int, List<ParameterRedirectAction>> actions, string fileName, int portalId, bool portalSpecific, ref List<string> messages)
{
if (messages == null)
{
messages = new List<string>();
}
if (File.Exists(fileName))
{
var rdr = new XmlTextReader(fileName)
{
XmlResolver = null,
DtdProcessing = DtdProcessing.Prohibit,
};
while (rdr.Read())
{
switch (rdr.NodeType)
{
case XmlNodeType.Element:
if (rdr.Name == "parameterRedirect")
{
var tabMessages = new List<string>();
int actionCount = 0;
// now set up the action
string portalIdRaw = rdr.GetAttribute("rulePortalId");
if (string.IsNullOrEmpty(portalIdRaw))
{
portalIdRaw = rdr.GetAttribute("portalId");
}
int rulePortalId = -1;
if (portalIdRaw != null)
{
int.TryParse(portalIdRaw, out rulePortalId);
}
if (rulePortalId == portalId || rulePortalId == -1 || portalSpecific)
{
// if portal specific, all rules are assumed to belong to the portal
string tabIdRaw = rdr.GetAttribute("tabIds");
string tabNames = rdr.GetAttribute("tabNames");
string name = rdr.GetAttribute("name");
string fromSiteRootRaw = rdr.GetAttribute("fromSiteRoot");
string fromDefaultRaw = rdr.GetAttribute("fromDefault");
string changeToSiteRootRaw = rdr.GetAttribute("changeToSiteRoot");
bool fromDefault;
bool fromSiteRoot;
bool changeToSiteRoot;
bool.TryParse(fromDefaultRaw, out fromDefault);
bool.TryParse(fromSiteRootRaw, out fromSiteRoot);
bool.TryParse(changeToSiteRootRaw, out changeToSiteRoot);
List<int> tabIds = XmlHelpers.TabIdsFromAttributes(tabIdRaw, tabNames, portalId, ref tabMessages);
foreach (int tabId in tabIds)
{
var action = new ParameterRedirectAction
{
PortalId = portalId,
LookFor = rdr.GetAttribute("lookFor"),
RedirectTo = rdr.GetAttribute("redirectTo"),
Name = name,
Action = rdr.GetAttribute("action"),
ChangeToSiteRoot = changeToSiteRoot,
TabId = tabId,
};
if (fromDefault)
{
// check for 'fromDefault' attribute
action.ForDefaultPage = true;
action.TabId = -2;
}
else
{
// or support the older convention, which was to include a tabid of -2
action.ForDefaultPage = tabId == -2;
}
if (fromSiteRoot)
{
action.TabId = -3; // site root marker
}
List<ParameterRedirectAction> tabActionCol;
if (actions.ContainsKey(action.TabId))
{
tabActionCol = actions[action.TabId];
}
else
{
tabActionCol = new List<ParameterRedirectAction>();
actions.Add(action.TabId, tabActionCol);
}
tabActionCol.Add(action);
actionCount++;
}
messages.Add(name + " redirect actions added:" + actionCount.ToString());
}
if (tabMessages.Count > 0)
{
messages.AddRange(tabMessages);
}
}
break;
case XmlNodeType.EndElement:
break;
}
}
rdr.Close();
}
}
public static void LoadFromXmlFile(this Dictionary<int, SharedList<ParameterRewriteAction>> actions, string fileName, int portalId, bool portalSpecific, ref List<string> messages)
{
if (messages == null)
{
messages = new List<string>();
}
if (File.Exists(fileName))
{
var rdr = new XmlTextReader(fileName)
{
XmlResolver = null,
DtdProcessing = DtdProcessing.Prohibit,
};
while (rdr.Read())
{
switch (rdr.NodeType)
{
case XmlNodeType.Element:
if (rdr.Name == "parameterRewrite")
{
string portalIdRaw = rdr.GetAttribute("portalId");
int rulePortalId = -1;
int actionCount = 0;
if (portalIdRaw != null)
{
int.TryParse(portalIdRaw, out rulePortalId);
}
if (rulePortalId == portalId || rulePortalId == -1 || portalId == -1 || portalSpecific)
{
// now set up the action
string tabIdRaw = rdr.GetAttribute("tabIds");
string tabNames = rdr.GetAttribute("tabNames");
string name = rdr.GetAttribute("name");
string fromSiteRootRaw = rdr.GetAttribute("fromSiteRoot");
bool fromSiteRoot;
bool.TryParse(fromSiteRootRaw, out fromSiteRoot);
List<int> tabIds = XmlHelpers.TabIdsFromAttributes(tabIdRaw, tabNames, portalId, ref messages);
foreach (int tabId in tabIds)
{
var action = new ParameterRewriteAction
{
LookFor = rdr.GetAttribute("lookFor"),
RewriteTo = rdr.GetAttribute("rewriteTo"),
Name = name,
TabId = tabId,
};
if (fromSiteRoot)
{
action.ForSiteRoot = true;
action.TabId = -3;
}
else
{
// older rule specified tabid -3 meant site root
action.ForSiteRoot = tabId == -3;
}
action.PortalId = portalId;
SharedList<ParameterRewriteAction> tabActionCol;
if (actions.ContainsKey(action.TabId))
{
tabActionCol = actions[action.TabId];
}
else
{
tabActionCol = new SharedList<ParameterRewriteAction>();
actions.Add(action.TabId, tabActionCol);
}
tabActionCol.Add(action);
actionCount++;
}
messages.Add(name + " rewrite actions added:" + actionCount.ToString());
}
}
break;
case XmlNodeType.EndElement:
break;
}
}
rdr.Close();
}
else
{
messages.Add("Filename does not exist:" + fileName);
}
}
public static Dictionary<string, string> CreateDictionaryFromString(string delimitedString, char pairSeparator, char separator)
{
var dictionary = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(delimitedString))
{
var pairs = delimitedString.Split(pairSeparator);
foreach (string pair in pairs)
{
if (!string.IsNullOrEmpty(pair))
{
var chars = pair.Split(separator);
dictionary[chars[0]] = chars[1];
}
}
}
return dictionary;
}
}
}